package com.android.xml;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class XMLDemoActivity extends Activity {
	Spinner spinner = null;
	Location location = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		spinner = new Spinner(this);

		ArrayList<String> lstAddress = null;

		location = getLocation();

		if (location == null) {
			TextView text = new TextView(this);
			text.setText("không tìm thấy Location");
			setContentView(text);
		} else {

			double lat = ((int) (location.getLatitude() * 1000000)) / 1000000.0;
			double lng = ((int) (location.getLongitude() * 1000000)) / 1000000.0;

			lstAddress = getAddress(lat, lng);

			if (lstAddress == null) {
				TextView text = new TextView(this);
				text.setText("không tìm thấy Dia Chi");
				setContentView(text);
			} else {
				ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(
						this, android.R.layout.simple_spinner_dropdown_item,
						lstAddress);

				spinner.setAdapter(spinnerAdapter);
				setContentView(spinner);
			}
		}
	}

	/*
	 * @Override protected void onCreate(Bundle savedInstanceState) { String url
	 * =
	 * "http://maps.google.com/maps/api/geocode/xml?latlng=16.458676,107.591818&sensor=true"
	 * ; SAXBuilder builder = new SAXBuilder(); StringBuilder string = new
	 * StringBuilder();
	 * 
	 * Log.i("MAPS URL : ", url);
	 * 
	 * try { Document doc = builder.build(url);
	 * 
	 * String path = "/GeocodeResponse/status";
	 * 
	 * List<?> resultNodes = XPath.selectNodes(doc, path); Iterator<?>
	 * iteratorResult = resultNodes.iterator();
	 * 
	 * while (iteratorResult.hasNext()) { string.append(((Element)
	 * iteratorResult.next()).getText()); }
	 * 
	 * TextView text = new TextView(this); text.setText(string.toString());
	 * 
	 * } catch (Exception e) { Log.e(this.getClass().getName(),
	 * "Lỗi mà ko bít vì răng"); }
	 * 
	 * }
	 */

	public Location getLocation() {
		try {
			LocationManager locationManager = (LocationManager) this
					.getSystemService(Context.LOCATION_SERVICE);
			LocationListener locationListener = new MyLocationListener();

			Criteria criteria = new Criteria();
			criteria.setAccuracy(Criteria.ACCURACY_FINE);

			String provider = locationManager.getBestProvider(criteria, true);

			locationManager.requestLocationUpdates(provider, 0, 0,
					locationListener);

			Location location = locationManager.getLastKnownLocation(provider);

			TextView text = new TextView(this);
			text.setText("(" + location.getLatitude() + ","
					+ location.getLongitude() + ")");

			setContentView(text);

			return location;
		} catch (Exception ex) {
			// Log.e(ex.getClass().getName(), ex.getMessage());
		}

		return null;
	}

	// @SuppressWarnings("rawtypes")
	public ArrayList<String> getAddress(double lat, double lng) {
		ArrayList<String> lstAddress = null;
		String url = "http://maps.google.com/maps/api/geocode/xml?latlng="
				+ lat + "," + lng + "&sensor=true";
		SAXBuilder builder = new SAXBuilder();

		Log.i("MAPS URL : ", url);

		try {
			Document doc = builder.build(url);

			Element root = doc.getRootElement();

			List<?> resultNodes = root.getChildren("result");
			Iterator<?> iteratorResult = resultNodes.iterator();

			lstAddress = new ArrayList<String>(resultNodes.size());

			while (iteratorResult.hasNext()) {
				Element addressNode = ((Element) iteratorResult.next())
						.getChild("formatted_address");

				lstAddress.add(addressNode.getText());
			}

		} catch (Exception e) {
			Log.e(e.getClass().getName(), e.getMessage());
		}

		return lstAddress;
	}

	class MyLocationListener implements LocationListener {

		@Override
		public void onLocationChanged(Location location) {
			XMLDemoActivity.this.location = location;
		}

		@Override
		public void onProviderDisabled(String provider) {
			Toast.makeText(XMLDemoActivity.this, provider + " is disable",
					Toast.LENGTH_LONG).show();
		}

		@Override
		public void onProviderEnabled(String provider) {
			Toast.makeText(XMLDemoActivity.this, provider + " is enable",
					Toast.LENGTH_LONG).show();
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {

		}

	}

}